package com.ophiux.FeverManagement.infratructure.repository;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.transaction.Transactional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.ophiux.FeverManagement.aplication.bean.getPatientTreatmentListByPatientResponse;
import com.ophiux.FeverManagement.domain.entity.RbPatientTreatment;
import com.ophiux.FeverManagement.domain.entity.RbPatientTreatmentDetail;
import com.ophiux.FeverManagement.domain.entity.RbPatientTreatmentDetailVisit;
import com.ophiux.FeverManagement.domain.entity.RbPatientTreatmentVisit;
import com.ophiux.FeverManagement.domain.repository.IPatientTreatmentRepository;
import com.ophiux.FeverManagement.infratructure.TableEnum;
import com.ophiux.FeverManagement.infratructure.mapper.RbPatientTreatmentDetailMapper;
import com.ophiux.FeverManagement.infratructure.mapper.RbPatientTreatmentMapper;
import com.ophiux.micro.core.infrastructure.TbableUniqueIDFactory;
import com.ophiux.utils.common.StringUtil;
import com.ophiux.utils.exception.OphiuxDaoException;
import com.ophiux.utils.exception.OphiuxRepositoryException;
import com.ophiux.utils.parser.AssemblyFactory;

import lombok.extern.slf4j.Slf4j;

@Repository
@Slf4j
public class PatientTreatmentRepository implements IPatientTreatmentRepository {

	@Autowired
	RbPatientTreatmentMapper patientTreatmentMapper;
	@Autowired
	RbPatientTreatmentDetailMapper patientTreatmentDetailMapper;
	
	@Override
	public String save(RbPatientTreatment model) throws OphiuxRepositoryException {
		return null;
	}
	
	@Transactional(rollbackOn=OphiuxRepositoryException.class)
	public String save(RbPatientTreatment model,List<RbPatientTreatmentDetail> model1) throws OphiuxRepositoryException {
		String ID = model.getID();
		try {
			if (StringUtil.isEmpty(ID)) {
				ID = TbableUniqueIDFactory.instance().createUniqueID(TableEnum.TABLE_SQE_RB_PATIENT_TREATMENT.toString());
				model.setID(ID);
				patientTreatmentMapper.insert(model);
				
				for (RbPatientTreatmentDetail rbPatientTreatmentDetail : model1) {
					rbPatientTreatmentDetail.setID(TbableUniqueIDFactory.instance().createUniqueID(TableEnum.TABLE_SQE_RB_PATIENT_TREATMENT_DETAIL.toString()));;
					rbPatientTreatmentDetail.setPATIENT_TREATMENT_ID(ID);
					rbPatientTreatmentDetail.setCreateInfo("");
					patientTreatmentDetailMapper.insert(rbPatientTreatmentDetail);
				}
				
			} else {
				model.setOperationInfo("");
				patientTreatmentMapper.update(model);
				
				//将之前的数据设置为无效
				RbPatientTreatmentDetail detail = new RbPatientTreatmentDetail();
				detail.setPATIENT_TREATMENT_ID(model.getID());
				detail.setStatusOff();
				patientTreatmentDetailMapper.update(detail);
				
				
				//新增数据
				for (RbPatientTreatmentDetail rbPatientTreatmentDetail : model1) {
					rbPatientTreatmentDetail.setID(TbableUniqueIDFactory.instance().createUniqueID(TableEnum.TABLE_SQE_RB_PATIENT_TREATMENT_DETAIL.toString()));;
					rbPatientTreatmentDetail.setPATIENT_TREATMENT_ID(ID);
					rbPatientTreatmentDetail.setCreateInfo("");
					patientTreatmentDetailMapper.insert(rbPatientTreatmentDetail);
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
			log.info("数据库执行异常,异常信息", e);
			throw new OphiuxRepositoryException(e);
		}
		return ID;
	}

	@Override
	@Transactional(rollbackOn=OphiuxRepositoryException.class)
	public void delete(Object uniqueId) throws OphiuxRepositoryException {
		try {
			RbPatientTreatment model = new RbPatientTreatment();
			model.setID(uniqueId.toString());
			model.setStatusOff();
			patientTreatmentMapper.update(model);
			
			RbPatientTreatmentDetail detail = new RbPatientTreatmentDetail();
			detail.setPATIENT_TREATMENT_ID(model.getID());
			detail.setStatusOff();
			patientTreatmentDetailMapper.update(detail);
		} catch (Exception e) {
			e.printStackTrace();
			log.info("数据库执行异常,异常信息", e);
			throw new OphiuxRepositoryException(e);
		}
	}

	@Override
	public RbPatientTreatment selectOneByKey(Object uniqueId) throws OphiuxRepositoryException {
		try {
			RbPatientTreatment model = patientTreatmentMapper.selectOneByKey(uniqueId.toString());
			return model;
		} catch (OphiuxDaoException e) {
			e.printStackTrace();
			log.info("数据库执行异常,异常信息", e);
			throw new OphiuxRepositoryException(e);
		}
	}

	@Override
	public RbPatientTreatment selectOne(Map map) throws OphiuxRepositoryException {

		return null;
	}

	@Override
	public List<RbPatientTreatment> selectAll(Map map) throws OphiuxRepositoryException {
		try {
			List<RbPatientTreatment> list = patientTreatmentMapper.selectAll(map);
			for (RbPatientTreatment rbPatientTreatment : list) {
				Map<String,String> detailMap = new HashMap<String,String>();
				detailMap.put("PATIENT_TREATMENT_ID", rbPatientTreatment.getID());
				List<RbPatientTreatmentDetail> detailList = patientTreatmentDetailMapper.selectAll(detailMap);
				rbPatientTreatment.setMEDICINE(detailList);
			}
			return list;
		} catch (Exception e) {
			e.printStackTrace();
			log.info("数据库执行异常,异常信息", e);
			throw new OphiuxRepositoryException(e);
		}
	}

	@Override
	public List<RbPatientTreatmentVisit> getAllDetaulVisit(Map map) throws OphiuxRepositoryException {
		try {
			List<RbPatientTreatment> list = patientTreatmentMapper.selectAll(map);

			List<RbPatientTreatmentVisit> visitList = AssemblyFactory.instance().parse(list, RbPatientTreatmentVisit.class);
			
			for (RbPatientTreatmentVisit rbPatientTreatment : visitList) {
				Map<String,String> detailMap = new HashMap<String,String>();
				detailMap.put("PATIENT_TREATMENT_ID", rbPatientTreatment.getID());
				detailMap.put("TREATMENT_ID", rbPatientTreatment.getTREATMENT_ID());
				List<RbPatientTreatmentDetailVisit> detailList = patientTreatmentDetailMapper.getAllDetaulVisit(detailMap);
				rbPatientTreatment.setMEDICINE(detailList);
			}
			return visitList;
		} catch (Exception e) {
			e.printStackTrace();
			log.info("数据库执行异常,异常信息", e);
			throw new OphiuxRepositoryException(e);
		}
	}

	/*@Override
	public List<RbPatientTreatment> selectAllByPatient(Map map) throws OphiuxRepositoryException {
		try {
			List<RbPatientTreatment> list = patientTreatmentMapper.selectAllByPatient(map);
			for (RbPatientTreatment rbPatientTreatment : list) {
				Map<String,String> detailMap = new HashMap<String,String>();
				detailMap.put("PATIENT_TREATMENT_ID", rbPatientTreatment.getID());
				List<RbPatientTreatmentDetail> detailList = patientTreatmentDetailMapper.selectAll(detailMap);
				rbPatientTreatment.setPATIENT_TREATMENT_DETAIL(detailList);
			}
			return list;
		} catch (Exception e) {
			e.printStackTrace();
			log.info("数据库执行异常,异常信息", e);
			throw new OphiuxRepositoryException(e);
		}
	}*/

}
